﻿using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace EasyDDD.Domain.User
{
    public class ApplicationRole : DomainEntity<Guid>, IAggregateRoot
    {
        [JsonConstructor]
        protected ApplicationRole() { }

        #region 属性
        /// <summary>
        /// 状态  0.正常 1.停用 2.待审核
        /// </summary>
        [Comment("状态")]
        public virtual short Status { get; set; } = 0;

        /// <summary>
        /// 角色类型 
        /// </summary>
        [Comment("角色类型")]
        public virtual int RoleType { get; set; } = 0;

        /// <summary>
        /// Gets or sets the name for this role.
        /// </summary>
        [Comment("角色名称")]
        [StringLength(50)]
        public virtual string Name { get; set; } = string.Empty;

        /// <summary>
        /// 角色标识
        /// </summary>
        [Comment("角色标识")]
        [Required]
        [StringLength(128)]
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 系统默认 0.否 1.是 不可删除
        /// </summary>
        [Comment("系统默认")]
        public virtual bool IsSystem { get; set; } = false;

        /// <summary>
        /// 备注
        /// </summary>
        [Comment("备注")]
        [StringLength(500)]
        public virtual string Remark { get; set; } = default!;
        #endregion

        #region 方法
        public void Add(string name, string code, string remark)
        {
            if (this.Id != Guid.Empty)
            {
                throw new InvalidOperationException($"该实体{this.GetType().Name}:{this.Id}已创建！");
            }
            Id = Guid.CreateVersion7();

            Code = code;
            Name = name;
            //NormalizedName = name.ToUpper();
            Remark = remark;
        }

        public void Set(string name, string code, string remark)
        {
            Code = code;
            Name = name;
            //NormalizedName = name.ToUpper();
            Remark = remark;
        }

        public void SetStatus(short status)
        {
            Status = status;
        }
        #endregion

        #region 聚合 ApplicationRoleClaim
        public virtual ICollection<ApplicationRoleClaim> RoleClaimList { get; set; } = default!;

        public void SetRoleClaim(string roleType, string oldRoleClaim, string newRoleClaim)
        {
            var entity = RoleClaimList.FirstOrDefault(e => e.RoleId == Id && e.ClaimValue == oldRoleClaim);
            if (entity != null)
            {
                entity.ClaimValue = newRoleClaim;
            }
            else
            {
                RoleClaimList.Add(new ApplicationRoleClaim()
                {
                    RoleId = Id,
                    ClaimType = roleType,
                    ClaimValue = newRoleClaim,
                });
            }
        }

        public void SetRoleClaim(ApplicationRoleClaim roleClaim)
        {
            var entity = RoleClaimList.FirstOrDefault(e => e.ClaimType == roleClaim.ClaimType && e.ClaimValue == roleClaim.ClaimValue);
            if (entity == null)
            {
                //roleClaim.Id = Guid.CreateVersion7();
                roleClaim.RoleId = Id;
                RoleClaimList.Add(roleClaim);
            }
        }

        public void SetRoleClaimList(ICollection<ApplicationRoleClaim> list)
        {
            foreach (var item in RoleClaimList.Where(e => !list.Any(f => f.ClaimType == e.ClaimType && f.ClaimValue == e.ClaimValue)))
            {
                RoleClaimList.Remove(item);
            }
            foreach (var item in list)
            {
                SetRoleClaim(item);
            }
        }
        #endregion
    }
}
